
const { Op } = require('sequelize')
const Address = require('../model/Address.model')

class AddressService {
    async createAddres(address) {
        return await Address.create(address)
    }
    async findAllAddr(user_id) {
        return await Address.findAll({
            attributes: ['id', 'consignee', 'phone', 'address', 'is_default'],
            where: { user_id }
        })
    }
    async updateAddr(id, addr) {
        return await Address.update(addr, { where: { id } })
    }
    async removeAddr(id) {
        return await Address.destroy({ where: { id } })
    }
    async setDefaultAddr(id, user_id) {
        // 先设置为0
        await Address.update({ is_default: false }, { where: { user_id } })
        // 在设置当前地址
        return await Address.update({ is_default: true }, { where: { id } })

    }
}

module.exports = new AddressService()